//
//  NetHardDisk_CreatFile_Task.m
//  EpointFrame5
//
//  Created by Pill Gong on 14-3-5.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "NetHardDisk_CreatFile_Task.h"
#import "Mail_Send_Task.h"
#import "SingleFile.h"

@implementation NetHardDisk_CreatFile_Task

- (void)execute {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *ValidateData = [EpointBizTools getToken];
    NSString *url = self.getMobileOAWebserviceURL;
    NSString *wsNamespace = NSLocalizedString(@"Epoint_DNet_WS_NameSpace", @"");
    NSString *userguid = [userDefaults objectForKey:Epoint_User_UserGuid];
    NSString *FileName = [params objectForKey:@"FileName"];
    NSString *ParentFolderGuid = [params objectForKey:@"ParentFolderGuid"];
    NSData *FileContent = [params objectForKey:@"FileContent"];
    NSString *FileContentLength = [params objectForKey:@"FileContentLength"];
    
    SingleFile *singleFile = [[SingleFile alloc] init];
    singleFile.ATTACHFILENAME=FileName;
    singleFile.fileData=FileContent;

    FileName = [EpointStringUtil filterXmlString:FileName];
    NSString *ParasXml = @"<![CDATA[<?xml version=\"1.0\" encoding=\"gb2312\"?><paras><UserGuid>%@</UserGuid><FileName>%@</FileName><ParentFolderGuid>%@</ParentFolderGuid><FileContentLength>%@</FileContentLength></paras>]]>";
    ParasXml = [NSString stringWithFormat:ParasXml,userguid,FileName,ParentFolderGuid,FileContentLength];
    
    if (![self isNetWorkAvailable])
    {
        [self exeFailureWithMessage:FailToLoadNetWorkPleaseCheckTheSetting];
    }
    else
    {
        //请求接口
        EpointWebServiceUtil *ws = [[EpointWebServiceUtil alloc]initWithUrl:url namespace:wsNamespace method:@"NetHardDisk_CreatFile"];
        [ws addParameterForString:@"ValidateData" value:ValidateData];
        [ws addParameterForString:@"ParasXml" value:ParasXml];
        NSString *bs =  ws.start;
        
        NSMutableArray *result = [self validateXMLWS2:bs];
        NSString *msg = [result objectAtIndex:1];
        if ([[result objectAtIndex:0]isEqualToString:@"1"]) {

           NSString*ClientGuid=  [EpointStringUtil getXMLFormatedAttribute:bs :@"ClientGuid"];
            NSString*AttachGuid=  [EpointStringUtil getXMLFormatedAttribute:bs :@"AttachGuid"];
            singleFile.AttachGuid=AttachGuid;
            NSMutableArray* list = [[NSMutableArray alloc] init];
            [list addObject:singleFile];
            Mail_Send_Task *upLoadTask=  [[Mail_Send_Task alloc] init];
            if ([upLoadTask upLoadAttachFile:list :ClientGuid :@"NetHardDisk"]) {
                [self sendDataRefreshUI:@[@"1", @"添加成功！"]];
            }else{
                 [self exeFailureWithMessage:@"附件上传失败！"];
            }
        }
        else
        {
            [self exeFailureWithMessage:msg];
        }
    }
}

@end
